package com.moma.modernartui;

import java.util.ArrayList;
import java.util.Collections;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.modernartui.R;


public class Mondrianoid extends Activity {

	private final String TAG = "ModernArtUI";

	private TextView hLayout1_tv1, hLayout1_tv2, hLayout2_tv1, hLayout2_tv2, hLayout2_tv3, hLayout3_tv1,hLayout3_tv2,hLayout3_tv3,hLayout3_tv4,hLayout3_tv5;
	private static final int COLOR_CANARY_YELLOW=Color.parseColor("#ffef00"); // http://encycolorpedia.com/ffef00
	private static final int COLOR_IRIS=Color.parseColor("#5a4fcf"); // http://encycolorpedia.com/5a4fcf
	private static final int COLOR_GRAY=Color.parseColor("#bebebe");//http://encycolorpedia.com/bebebe
	private static final int COLOR_GUPPIEGREEN=Color.parseColor("#00ff7f");//http://encycolorpedia.com/00ff7f
	private static final int COLOR_HOMEDEPOT=Color.parseColor("#ee7125");//http://encycolorpedia.com/ee7125
	private static final int COLOR_YELP_RED=Color.parseColor("#c41200");//http://encycolorpedia.com/c41200
	private static final int COLOR_YELP_ENGLISH_LAVENDER=Color.parseColor("#b48395");//http://encycolorpedia.com/a56b86
	private static final int COLOR_YELP_LINKEDIN=Color.parseColor("#0e76a8");//http://encycolorpedia.com/0e76a8
	private static final int COLOR_YELP_MAGENTA=Color.parseColor("#dba1ce");//http://encycolorpedia.com/dba1ce
	private static final int COLOR_YELP_FUSCIA=Color.parseColor("#8e3463");//http://encycolorpedia.com/c41200
	private static final int COLOR_YELP_OLIVE=Color.parseColor("#808000");//http://encycolorpedia.com/c41200
	private static final int COLOR_YELP_RUBINERED=Color.parseColor("#d10056");//http://encycolorpedia.com/c41200
	private static final int COLOR_CRIMSON=Color.parseColor("#dc143c");//http://encycolorpedia.com/c41200

	private static final int COLOR_YALEBLUE=Color.parseColor("#0f4d92");//http://encycolorpedia.com/c41200
	private static final int COLOR_FACEBOOK=Color.parseColor("#3b5998");//http://encycolorpedia.com/c41200
	private static final int COLOR_DAFFODIL=Color.parseColor("#ffff31");//http://encycolorpedia.com/c41200
	private static final int COLOR_TROPICAL=Color.parseColor("#00755e");//http://encycolorpedia.com/c41200
	private static final int COLOR_YAHOO=Color.parseColor("#720e9e");//http://encycolorpedia.com/c41200
	private static final int COLOR_PERSIMONN=Color.parseColor("#ec5800");//http://encycolorpedia.com/c41200
	private static final int COLOR_INDIAGREEN=Color.parseColor("#138808");//http://encycolorpedia.com/c41200
	private static final int COLOR_ASHGREY=Color.parseColor("#b2beb5");//http://encycolorpedia.com/c41200
	private static final int COLOR_STRAWBERRY=Color.parseColor("#fc5a8d");//http://encycolorpedia.com/c41200
	private static final int COLOR_ZOOTOOL=Color.parseColor("#5e8b1d");//http://encycolorpedia.com/c41200
	private static final int COLOR_COPPERRED=Color.parseColor("#cb6d51");//http://encycolorpedia.com/c41200
	private static final int COLOR_PEACOCK=Color.parseColor("#266363");//http://encycolorpedia.com/c41200
	private static final int COLOR_SOFTONIC=Color.parseColor("#008ace");//http://encycolorpedia.com/c41200
	
	
	private static final int PROGRESS_MULTIPLIER=0x10000;

	private static final String QUOTE_1="DECIDE.COMMIT.SUCCEED";
	private static final String QUOTE_2="Live LIFE in COLOR";
	private static final String QUOTE_3="Intellect confuses Intuition";
	private static final String QUOTE_4="Effort is Attractive";
	private static final String QUOTE_5="When in Doubt,Vacation";
	private static final String QUOTE_6="Silence is Golden";
	private static final String QUOTE_7="Pick Your Battles";
	private static final String QUOTE_8="Talk is cheap";
	private static final String QUOTE_9="Envy breeds hate.";
	private static final String QUOTE_10="Seize the day";
	private static final String QUOTE_11="Damn the torpedoes";

	//	It is possible that, through horizontal and vertical lines constructed with awareness, but not with calculation, led by high intuition, and brought to harmony and rhythm, these basic forms of beauty, supplemented if necessary by other direct lines or curves, can become a work of art, as strong as it is true. (Piet Mondrian)

	ArrayList<String> quotes= new ArrayList<String>();
	{
		quotes.add(QUOTE_1);
		quotes.add(QUOTE_2);
		quotes.add(QUOTE_3);
		quotes.add(QUOTE_4);
		quotes.add(QUOTE_5);
		quotes.add(QUOTE_6);
		quotes.add(QUOTE_7);
		quotes.add(QUOTE_8);
		quotes.add(QUOTE_9);
		quotes.add(QUOTE_10);
		quotes.add(QUOTE_11);
		
	}

	ArrayList<Integer> colors = new ArrayList<Integer>();
	{
		colors.add(COLOR_COPPERRED);
		colors.add(COLOR_ZOOTOOL);
		colors.add(COLOR_STRAWBERRY);
		colors.add(COLOR_ASHGREY);
		colors.add(COLOR_PERSIMONN);
		colors.add(COLOR_INDIAGREEN);
		colors.add(COLOR_YAHOO);
		colors.add(COLOR_TROPICAL);
		colors.add(COLOR_DAFFODIL);
		colors.add(COLOR_FACEBOOK);
		colors.add(COLOR_YALEBLUE);
		colors.add(COLOR_CRIMSON);
		colors.add(COLOR_PEACOCK);
		colors.add(COLOR_SOFTONIC);


		colors.add(COLOR_CANARY_YELLOW);
		colors.add(COLOR_IRIS);
		colors.add(COLOR_HOMEDEPOT);
		colors.add(COLOR_YELP_RED);
		colors.add(COLOR_YELP_ENGLISH_LAVENDER);
		colors.add(COLOR_GUPPIEGREEN);
		colors.add(COLOR_YELP_LINKEDIN);
		colors.add(COLOR_YELP_MAGENTA);
		colors.add(COLOR_YELP_FUSCIA);
		colors.add(COLOR_YELP_OLIVE);
		colors.add(COLOR_YELP_RUBINERED);
	}	

	private SeekBar seek;
	private static final String MOMA_URL = "http://www.moma.org";	
	private static final int MENU_MORE_INFO = Menu.FIRST;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		hLayout1_tv1 = (TextView) findViewById(R.id.textView1);
		hLayout1_tv2 = (TextView) findViewById(R.id.textView2);

		hLayout2_tv1 = (TextView) findViewById(R.id.textView3);
		hLayout2_tv2 = (TextView) findViewById(R.id.textView4);
		hLayout2_tv3 = (TextView) findViewById(R.id.textView5);

		hLayout3_tv1 = (TextView) findViewById(R.id.textView6);
		hLayout3_tv2 = (TextView) findViewById(R.id.textView7);
		hLayout3_tv3 = (TextView) findViewById(R.id.textView8);
		hLayout3_tv4 = (TextView) findViewById(R.id.textView9);
		hLayout3_tv5 = (TextView) findViewById(R.id.textView10);

		hLayout2_tv1.setBackgroundColor(COLOR_GRAY);
		hLayout3_tv4.setBackgroundColor(COLOR_GRAY);



		shuffleMondrian();

		seek = (SeekBar) findViewById(R.id.seekBar1);
		seek.setOnSeekBarChangeListener(seekBarChangeListener);



	}

	private SeekBar.OnSeekBarChangeListener seekBarChangeListener
	= new SeekBar.OnSeekBarChangeListener() {


		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}	
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			if (progress==0){
				shuffleMondrian();
			}else{
				//retain the hLayout2_tv3 color for the action bar background color 
				ActionBar actionBar = getActionBar();
				ColorDrawable colorDrawable = new ColorDrawable(colors.get(2).intValue()-  (progress * PROGRESS_MULTIPLIER));
				actionBar.setBackgroundDrawable(colorDrawable);

				hLayout1_tv1.setBackgroundColor(colors.get(0).intValue() -  (progress * PROGRESS_MULTIPLIER));
				hLayout1_tv2.setBackgroundColor(colors.get(1).intValue() -  (progress * PROGRESS_MULTIPLIER));

				hLayout2_tv2.setBackgroundColor(colors.get(0).intValue()-  (progress * PROGRESS_MULTIPLIER));
				hLayout2_tv3.setBackgroundColor(colors.get(2).intValue()-  (progress * PROGRESS_MULTIPLIER));

				hLayout3_tv1.setBackgroundColor(colors.get(3).intValue()-  (progress * PROGRESS_MULTIPLIER));
				hLayout3_tv2.setBackgroundColor(colors.get(2).intValue()-  (progress * PROGRESS_MULTIPLIER));
				hLayout3_tv3.setBackgroundColor(colors.get(4).intValue()-  (progress * PROGRESS_MULTIPLIER));
				hLayout3_tv5.setBackgroundColor(colors.get(3).intValue() -  (progress * PROGRESS_MULTIPLIER));
			}
		}


	};

	private void shuffleMondrian() {
		//shuffle  all colors , except the grays
		Collections.shuffle(colors);
		Collections.shuffle(quotes);

		//match the action bar background color with that of hLayout2_tv3
		ActionBar actionBar = getActionBar();
		ColorDrawable colorDrawable = new ColorDrawable(colors.get(2).intValue());
		actionBar.setBackgroundDrawable(colorDrawable);
		actionBar.setTitle(quotes.get(0));
		hLayout1_tv1.setBackgroundColor(colors.get(0).intValue());
		hLayout1_tv2.setBackgroundColor(colors.get(1).intValue());

		hLayout2_tv2.setBackgroundColor(colors.get(0).intValue());
		hLayout2_tv3.setBackgroundColor(colors.get(2).intValue());

		hLayout3_tv1.setBackgroundColor(colors.get(3).intValue());
		hLayout3_tv2.setBackgroundColor(colors.get(2).intValue());
		hLayout3_tv3.setBackgroundColor(colors.get(4).intValue());
		hLayout3_tv5.setBackgroundColor(colors.get(3).intValue());
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(Menu.NONE, MENU_MORE_INFO, Menu.NONE,R.string.more_info);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_MORE_INFO:
			openDialog();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void openDialog(){

		AlertDialog.Builder builder = new AlertDialog.Builder(Mondrianoid.this);
		builder
		.setMessage( R.string.dialog_msg)
		.setPositiveButton(R.string.visit_moma,
				new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				Uri webpage = Uri.parse(MOMA_URL);
				startActivity(new Intent(Intent.ACTION_VIEW, webpage));
			}
		})
		.setNeutralButton(R.string.not_now, null);
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
		alertDialog.getWindow().setLayout(800, 600);
		alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

	}



}
